package com.ruoyi.warehouse.service.impl;

import java.sql.SQLException;
import java.util.List;

import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.CodeClient;
import com.ruoyi.warehouse.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.warehouse.mapper.LlrkIncomingMaterialStorageMapper;
import com.ruoyi.warehouse.service.ILlrkIncomingMaterialStorageService;

import javax.annotation.Resource;

/**
 * 来料入库Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-06-19
 */
@Service
public class LlrkIncomingMaterialStorageServiceImpl implements ILlrkIncomingMaterialStorageService
{
    @Resource
    private LlrkIncomingMaterialStorageMapper llrkIncomingMaterialStorageMapper;
    @Resource
    private CodeClient codeClient;

    /**
     * 查询来料入库
     * 
     * @param llCode 来料入库主键
     * @return 来料入库
     */
    @Override
    public LlrkIncomingMaterialStorage selectLlrkIncomingMaterialStorageByLlCode(String llCode)
    {
        return llrkIncomingMaterialStorageMapper.selectLlrkIncomingMaterialStorageByLlCode(llCode);
    }

    /**
     * 查询来料入库列表
     * 
     * @param llrkIncomingMaterialStorage 来料入库
     * @return 来料入库
     */
    @Override
    public List<LlrkIncomingMaterialStorage> selectLlrkIncomingMaterialStorageList(LlrkIncomingMaterialStorage llrkIncomingMaterialStorage)
    {
        return llrkIncomingMaterialStorageMapper.selectLlrkIncomingMaterialStorageList(llrkIncomingMaterialStorage);
    }

    /**
     * 审核
     * 
     * @param llCode 审核
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCkxqStorage(String llCode)
    {
        LlrkIncomingMaterialStorage llrkIncomingMaterialStorage = llrkIncomingMaterialStorageMapper.selectLlrkIncomingMaterialStorageByLlCode(llCode);
        llrkIncomingMaterialStorageMapper.updateLlrkIsUseStorage(llCode);
        return llrkIncomingMaterialStorageMapper.insertCkxqStorage(llrkIncomingMaterialStorage);
    }

    /**
     * 撤销
     * */
    @Transactional(rollbackFor = Exception.class)
    public int cancel(String llCode){
        //根据编码查出来料单数据
        LlrkIncomingMaterialStorage llrkIncomingMaterialStorage = llrkIncomingMaterialStorageMapper.selectLlrkIncomingMaterialStorageByLlCode(llCode);

        // 获取来料单中的来料详情
        List<LlslIncomingMaterials> llslIncomingMaterials = llrkIncomingMaterialStorage.getLlslIncomingMaterialsList();
        if (StringUtils.isNotNull(llslIncomingMaterials))
        {
            for (LlslIncomingMaterials llslIncomingMaterials1 : llslIncomingMaterials)
            {
                // 补全来料详情信息并判断是否撤销成功
                llslIncomingMaterials1.setLlrkIncomingMaterialStorage(llrkIncomingMaterialStorage);
                int i = llrkIncomingMaterialStorageMapper.updateCkxqStorage(llslIncomingMaterials1);
                // 不成功报错
                if(i==0){
                    throw new RuntimeException("库存不足，撤销失败");
                }
            }
        }
        // 撤销成功，修改状态
        return llrkIncomingMaterialStorageMapper.updateLlrkIsUseCancel(llCode);
    }

    @Transactional
    @Override
    public int insertLlrkIncomingMaterialStorage(LlrkIncomingMaterialStorage llrkIncomingMaterialStorage)
    {
        String code = codeClient.getCode(2);
        //编码和记录人赋值
        llrkIncomingMaterialStorage.setLlCode(code);
        llrkIncomingMaterialStorage.setLlrkPersonnel(SecurityUtils.getUsername());
        int rows = llrkIncomingMaterialStorageMapper.insertLlrkIncomingMaterialStorage(llrkIncomingMaterialStorage);
//        //调用来料数量添加方法
        insertLlslIncomingMaterials(llrkIncomingMaterialStorage);
        return rows;
//        return 0;
    }

    /**
     * 修改来料入库
     * 
     * @param llrkIncomingMaterialStorage 来料入库
     * @return 结果
     */
    @Transactional
    @Override
    public int updateLlrkIncomingMaterialStorage(LlrkIncomingMaterialStorage llrkIncomingMaterialStorage)
    {
        llrkIncomingMaterialStorageMapper.deleteLlslStorage(llrkIncomingMaterialStorage.getLlCode());
        insertLlslIncomingMaterials(llrkIncomingMaterialStorage);
        return llrkIncomingMaterialStorageMapper.updateLlrkIncomingMaterialStorage(llrkIncomingMaterialStorage);
    }

    /**
     * 批量删除来料入库
     * 
     * @param llCodes 需要删除的来料入库主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteLlrkIncomingMaterialStorageByLlCodes(String[] llCodes)
    {
        return llrkIncomingMaterialStorageMapper.deleteLlrkIncomingMaterialStorageByLlCodes(llCodes);
    }

    /**
     * 删除来料入库信息
     * 
     * @param llCode 来料入库主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteLlrkIncomingMaterialStorageByLlCode(String llCode)
    {
        return llrkIncomingMaterialStorageMapper.deleteLlrkIncomingMaterialStorageByLlCode(llCode);
    }

    /**
     * 新增物料档案信息
     * 
     * @param llrkIncomingMaterialStorage 来料入库对象
     */
    public void insertLlslIncomingMaterials(LlrkIncomingMaterialStorage llrkIncomingMaterialStorage)
    {
        List<LlslIncomingMaterials> llslIncomingMaterials = llrkIncomingMaterialStorage.getLlslIncomingMaterialsList();
        String llCode = llrkIncomingMaterialStorage.getLlCode();
        if (StringUtils.isNotNull(llslIncomingMaterials))
        {
            List<LlslIncomingMaterials> list = new ArrayList<>();
            for (LlslIncomingMaterials llslIncomingMaterials1 : llslIncomingMaterials)
            {
                llslIncomingMaterials1.setLlCode(llCode);
                list.add(llslIncomingMaterials1);
            }
            if (list.size() > 0)
            {
                llrkIncomingMaterialStorageMapper.insertLlslIncomingMaterialsStorage(list);
            }
        }
    }

    /**
     * 查询物料档案信息
     *
     * @param wldaMaterialFile 来料入库对象
     */
    @Override
    public List<WldaMaterialFile> selectWldaMaterialFile(WldaMaterialFile wldaMaterialFile) {
        return llrkIncomingMaterialStorageMapper.selectWldaMaterialFile(wldaMaterialFile);
    }

    /**
     * 查询仓库信息
     *
     * @param ckWarehouse 需要删除的数据主键集合
     * @return 结果
     */
    public List<CkWarehouse> selectCkWarehouse(CkWarehouse ckWarehouse){
        return llrkIncomingMaterialStorageMapper.selectCkWarehouse(ckWarehouse);
    }

}
